package com.spring.security;

import com.permission.dao.PerResourceDao;
import com.permission.dao.PerUserDao;
import com.permission.enumeration.PerUserType;
import com.permission.model.PerResource;
import com.permission.model.PerRoleResource;
import com.permission.model.PerUser;
import com.permission.model.PerUserRole;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 14-5-12
 * Time: 下午2:34
 * To change this template use File | Settings | File Templates.
 */
public class MyUserDetailsService implements UserDetailsService {
    @Resource
    private PerUserDao perUserDao;

    @Resource
    private PerResourceDao perResourceDao;

    @Transactional(readOnly = true)
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        PerUser perUser=null;
        Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
        if(username.equals("admin")){
            perUser=new PerUser();
            perUser.setUsername(username);
            perUser.setEnabled(true);
            perUser.setPassword("6f4e289844c688bcca2578b16cdbb1186e5bce8c77ab006a189115435cbff95401c2a74fb7e02189");
            List<PerResource> perResources = this.perResourceDao.listByHql();
            for (PerResource perResource : perResources) {
                grantedAuthorities.add(new SimpleGrantedAuthority(perResource.getCode()));
            }
            perUser.setAuthorities(grantedAuthorities);
        }else{
            perUser = this.perUserDao.findUniqueObjectByHql("from PerUser pu where pu.username=?", new Object[]{username});
            if(perUser.getType().equals(PerUserType.admin)){
                List<PerResource> perResources = this.perResourceDao.listByHql();
                for (PerResource perResource : perResources) {
                    grantedAuthorities.add(new SimpleGrantedAuthority(perResource.getCode()));
                }
            }else{
                Set<PerUserRole> perUserRoles = perUser.getPerUserRoles();
                for (PerUserRole perUserRole : perUserRoles) {
                    Set<PerRoleResource> perRoleResources = perUserRole.getPerRole().getPerRoleResources();
                    for (PerRoleResource perRoleResource : perRoleResources) {
                        grantedAuthorities.add(new SimpleGrantedAuthority(perRoleResource.getPerResource().getCode()));
                    }
                }
                perUser.setAuthorities(grantedAuthorities);
            }
        }
        return perUser;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
